<script>
    import Vue from 'vue';
    import { mapMutations, mapActions, mapState } from 'vuex';
    import Wechat from './common/wechat/wechat';

	export default {
        methods: {
            //应用初始化,获取模板,获取页面路由,获取用户信息,保存用户Token并返回初始进入页面
            ...mapActions(['getTemplate', 'getRoutes', 'getUserInfo', 'setTokenAndBack']),
            // 获取系统栏高度
            async setAppInfo() {
                let that = this;
                uni.getSystemInfo({
                    success:function(e){
                        Vue.prototype.statusBar = e.statusBarHeight
                        // #ifndef MP
                        if(e.platform == 'android') {
                            Vue.prototype.customBar = e.statusBarHeight + 50
                        }else {
                            Vue.prototype.customBar = e.statusBarHeight + 45
                        }
                        // #endif
                        // #ifdef MP-WEIXIN
                        let custom = that.$wx.getMenuButtonBoundingClientRect()
                        Vue.prototype.customBar = custom.bottom + custom.top - e.statusBarHeight
                        // #endif

                        // #ifdef MP-ALIPAY
                        Vue.prototype.customBar = e.statusBarHeight + e.titleBarHeight
                        // #endif
                    }
                })
            },
            getQueryString(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if(r != null) {
                    return decodeURIComponent(r[2]);
                }
                return '';
            }
        },
        onLaunch: async function(options) {
            if (this.getQueryString('token')) {
                // this.store.dispatch('setTokenAndBack', this.getQueryString('token'))
                uni.setStorageSync('token', this.getQueryString('token'))
                uni.setStorageSync('session_key', this.getQueryString('sessionKey'));
                uni.setStorageSync('openid', this.getQueryString('openid'));
                this.$store.dispatch('setTokenAndBack', this.getQueryString('token'))
                this.$store.dispatch('getUserInfo').then(res => {
                    store.commit('LOGIN_TIP', false);
                    store.commit('USER_INFO', res.data);
                    uni.setStorageSync('userInfo', res.data);
                }).catch(error => {})
            } else {
                uni.removeStorageSync('token');
            }
            await this.setAppInfo();
            await this.getTemplate(options);
            await this.getRoutes();
        },
        onShow: function() {
            this.$store.commit('CART_NUM');
        },
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	@import 'static/css/main.css';
	/*每个页面公共css */
</style>
